<template>
    <el-dialog v-if="dialogVisible" :close-on-click-modal='false' title="脚本地址" :visible.sync="dialogVisible" @close="resetForm('addScriptFileRef')" width="400px">
        <el-form label-position="top" label-width="auto" size="medium" :hide-required-asterisk='true' :model="addScriptFileForm" :rules="addScriptFileRules" ref="addScriptFileRef" v-loading="loading">
            <el-form-item label="" prop="material">
                <el-input type="textarea" :rows="3" placeholder="请输入脚本地址，例如：https://www.baidu.com/" v-model="addScriptFileForm.material">
                </el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="resetForm('addScriptFileRef')" class="el-button-width" size="small">取 消</el-button>
            <el-button type="primary" @click="submitForm('addScriptFileRef')" class="el-button-width" size="small" :disabled='disabled' :loading="disabled">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
export default {
    name: "",
    data() {
        return {
            disabled: false,
            dialogVisible: false,
            addScriptFileForm: {
                id: "",
                material: "",
            },
            addScriptFileRules: {
                material: [
                    {
                        required: true,
                        message: "请选择脚本地址",
                        trigger: "blur",
                    },
                ],
            },
        };
    },

    created() {
        this.$bus.on("addScriptFile", (value) => {
            this.addScriptFileForm.id = value.id
            this.dialogVisible = value.dialogVsb;
        });
    },
    beforeDestroy() {
        this.$bus.off("addScriptFile");
    },
    methods: {
        // 提交添加
        submitForm(formName) {
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    this.disabled = true;
                    const { data: res } = await this.$http.post(
                        "adminapi/demandhz/scriptUpload",
                        this.addScriptFileForm
                    );
                    if (res.code !== 200) {
                        this.disabled = false;
                        this.$message.error(res.msg);
                        return;
                    } else {
                        this.$message({
                            message: "提交成功",
                            type: "success",
                        });
                        this.disabled = false;
                        this.resetForm(formName);
                        this.$parent.getDataList(this.$parent.setpage);
                    }
                } else {
                    this.disabled = false;
                    return false;
                }
            });
        },

        // 关闭重置表单
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.dialogVisible = false;
        },
    },
};
</script>
<style scoped>

</style>

